from pypinyin import pinyin, Style
import re

class Commander():
    def __init__(self,wakePattern="",sleepPattern="") -> None:
        self.wakePattern = wakePattern
        self.sleepPattern = sleepPattern

    def match_cmd(self,voiceStr,pattern) -> bool:
        if not voiceStr:
            return False
        voiceStrPinyin = self.chinese_to_pinyin(voiceStr)
        print(f"pinyin:{voiceStrPinyin}")
        if re.fullmatch(pattern,voiceStrPinyin):
            return True
        else:
            return False

    def chinese_to_pinyin(self,command):
        pinyin_list = pinyin(command, style=Style.NORMAL)  # 获取首字母拼音
        return ''.join([item[0] for item in pinyin_list])  # 将拼音合并为字符串